package com.jianxiu.bean;

import java.util.ArrayList;

/**
 * Created by tancw on 2017/3/23.
 */
public class PageIndex {
    private long startindex;
    private long endindex;
    private Object[] arry;

    public PageIndex(long endindex, long startindex) {
        this.endindex = endindex;
        this.startindex = startindex;
        ArrayList list = new ArrayList();
        for (long i = startindex; i <= endindex; ++i) {
            list.add(Long.valueOf(i));
        }
        this.arry = new Object[list.size()];
        this.arry = list.toArray();
    }

    public long getStartindex() {
        return startindex;
    }

    public void setStartindex(long startindex) {
        this.startindex = startindex;
    }

    public long getEndindex() {
        return endindex;
    }

    public void setEndindex(long endindex) {
        this.endindex = endindex;
    }

    public Object[] getArry() {
        return arry;
    }

    public void setArry(Object[] arry) {
        this.arry = arry;
    }

    public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage) {
        long startpage = (long) currentPage - (viewpagecount % 2L == 0L ? viewpagecount / 2L - 1L : viewpagecount / 2L);
        long endpage = (long) currentPage + viewpagecount / 2L;
        if (startpage < 1L) {
            startpage = 1L;
            if (totalpage >= viewpagecount) {
                endpage = viewpagecount;
            } else {
                endpage = totalpage;
            }
        }
        if (endpage > totalpage) {
            endpage = totalpage;
            if (totalpage - viewpagecount > 0L) {
                startpage = totalpage - viewpagecount + 1L;
            } else {
                startpage = 1L;
            }
        }
        return new PageIndex(startpage, endpage);
    }
}
